package com.wenjh.jdmzt.android.activity;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.umeng.fb.UMFeedbackService;
import com.umeng.fb.ui.FeedbackConversations;
import com.umeng.fb.util.FeedBackListener;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.wenjh.jdmzt.R;
import com.wenjh.jdmzt.android.view.MAlertListDialog;
import com.wenjh.jdmzt.android.view.MProgressDialog;
import com.wenjh.jdmzt.android.view.OnItemSelectedListener;

public class SettingActivity extends BaseActivity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_about);
		initViews();
		initEvents();
		
		FeedBackListener listener = new FeedBackListener() {
		    @Override
		    public void onSubmitFB(Activity activity) {
		        EditText phoneText = (EditText) activity
		                .findViewById(R.id.feedback_phone);
		        Map<String, String> contactMap = new HashMap<String, String>();
		        contactMap.put("phone", phoneText.getText()
		                .toString());
		        UMFeedbackService.setContactMap(contactMap);
		    }
		    @Override
		    public void onResetFB(Activity activity,
		            Map<String, String> contactMap,
		            Map<String, String> remarkMap) {
		        EditText phoneText = (EditText) activity
		                .findViewById(R.id.feedback_phone);
		        if (contactMap != null) {
		            phoneText.setText(contactMap.get("phone"));
		        }
		    }
		};
		UMFeedbackService.setFeedBackListener(listener);    
		
	}
	
	@Override
	protected void initEvents() {
		findViewById(R.id.setting_layout_checknewversion).setOnClickListener(this);
		findViewById(R.id.setting_layout_about).setOnClickListener(this);
		findViewById(R.id.setting_layout_feedback).setOnClickListener(this);
		findViewById(R.id.setting_layout_myfeedback).setOnClickListener(this);
	}

	@Override
	protected void initViews() {
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.setting_layout_about:
			MAlertListDialog dialog = new MAlertListDialog(this, new String[]{"发邮件", "复制邮箱地址"});
			dialog.setOnItemSelectedListener(new OnItemSelectedListener() {
				@Override
				public void onItemSelected(int index) {
					TextView emailView = (TextView) findViewById(R.id.setting_tv_email);
					switch (index) {
					case 0:
						Intent intent=new Intent(android.content.Intent.ACTION_SEND);
		                intent.setType("message/rfc822");
		                intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailView.getText().toString()});
		                startActivity(Intent.createChooser(intent, "选择邮件客户端"));
						break;
					case 1:
						ClipboardManager clipboardManager= (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); 
						clipboardManager.setText(emailView.getText().toString()); 
						toast("已复制到粘贴板");
						break;

					default:
						break;
					}
				}
			});
			showDialog(dialog);
			break;
		case R.id.setting_layout_checknewversion:
			showDialog(new MProgressDialog(this));
			UmengUpdateAgent.update(this);
			UmengUpdateAgent.setUpdateOnlyWifi(false);
			UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
		        @Override
		        public void onUpdateReturned(int updateStatus,UpdateResponse updateInfo) {
		        	closeDialog();
		            switch (updateStatus) {
		            case 1: // has no update
		            	toast("当前已经是最新的版本");
		                break;
		            case 2: // none wifi
		            	toast("没有wifi连接， 只在wifi下更新");
		                break;
		            case 3: // time out
		            	toast("网络请求超时");
		                break;
		            }
		        }
		});
			break;
		case R.id.setting_layout_feedback:
			UMFeedbackService.openUmengFeedbackSDK(this);
			break;
		case R.id.setting_layout_myfeedback:
			startActivity(new Intent(getApplicationContext(), FeedbackConversations.class));
			break;
		default:
			break;
		}
	}

}
